//
//  HHListSample.swift
//  HHSwiftuiPractice
//
//  Created by YL-Hong on 2023/10/8.
// List EditMode

import SwiftUI

struct HHListSample: View {
    @State var fruits: [String] = [
           "🍎", "🍊", "🍇", "🍓"
       ]
       
       var vegetables: [String] = [
           "🥦", "🥒", "🧅", "🥬"
       ]
    
    @State  var isEditMode: EditMode = EditMode.inactive

    var body: some View {
        
        NavigationView {
            List{
                Section {
                    //The key path to the provided data's identifier.
                    //key path 路径写法 加上前缀 \.
                    
                    ForEach(fruits, id: \.self) { fruit in
                        
                        NavigationLink {
                            Text(fruit)
                        } label: {
                            HStack {
                                 Text(fruit)
                                     .frame(width: 50, height: 50)
                                     .font(.title)
                                     .background(
                                         Circle()
                                             .fill(Color.green.opacity(0.3))
                                     )
                                 Text("\(Int.random(in: 1..<10)) kg")
                                     .font(.subheadline)
                             }
                        }

                       
                    }
                    .onDelete(perform: delete(indexSet:))
                    .onMove { indexSet, index in
                        move(indexSet: indexSet, index: index)
                    }
                    
                    
                }header: {
                    HStack {
                        Text("🥑")
                        Text("fruit".capitalized)
                            .foregroundColor(Color.black)
                        Text("(\(fruits.count))")
                            .font(.subheadline)
                            .fontWeight(.semibold)
                        
                    }
                } footer: {
                    Text("🦶 fruit")
                }
                
                Section {
                    ForEach(vegetables, id: \.self) {  vegetable  in
                                HStack {
                                    Circle()
                                        .fill(Color.yellow.opacity(0.3))
                                        .frame(width: 50, height: 50)
                                        .overlay(
                                            Text(vegetable)
                                                .font(.title)
                                        )
                                    Text("\(Int.random(in: 1..<10)) kg")
                                        .font(.subheadline)
                                }
                            }
                            .listRowBackground(Color.yellow.opacity(0.1))
                        } header: {
                            HStack {
                                Text("🥕")
                                Text("vegetable".capitalized)
                                    .foregroundColor(Color.black)
                                
                                Text("(\(vegetables.count))")
                                    .font(.subheadline)
                                    .fontWeight(.semibold)
                            }
                        } footer: {
                            Text("🦶 vegetable")
                        }

            }
            .toolbar {
                
                //编辑模式
                //自定义 编辑按钮
                ToolbarItem(placement: .navigationBarLeading) {
                    Button {

                        switch isEditMode {
                            case .active:
                              self.isEditMode = .inactive
                            case .inactive:
                              self.isEditMode = .active
                            default:
                              break
                          }

                    } label: {
                        Text(isEditMode.isEditing ? "完成" : "编辑")
                    }
                    
//                    EditButton()

                }
                
                ToolbarItem(placement: .navigationBarTrailing) {
                    Button("Add") {
                        fruits.append("🥝")
                        }
                }
            }
            .environment(\.editMode, $isEditMode)
            .listStyle(.sidebar)
            .navigationTitle(Text("Grocery list"))
            .navigationBarTitleDisplayMode(.automatic)
        
            
        }
       
        
    }
    
    
  func delete(indexSet: IndexSet) {
       fruits.remove(atOffsets: indexSet)
   }
   
   func move(indexSet: IndexSet, index: Int) {
       fruits.move(fromOffsets: indexSet, toOffset: index)
   }
    
}

struct HHListSample_Previews: PreviewProvider {
    static var previews: some View {
        HHListSample()
    }
}
